package com.app.mvvmkotlindemo.base

import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.provider.Settings
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.databinding.ViewDataBinding
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.app.mvvmkotlindemo.R
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.everyline.baselibrary.base.activity.BaseVMDbActivity
import com.everyline.baselibrary.base.viewmodel.BaseViewModel
import com.everyline.baselibrary.ext.KtxActivityManger
import com.everyline.baselibrary.ext.dismissLoadingExt
import com.everyline.baselibrary.ext.showLoadingExt
import com.everyline.baselibrary.network.ApiException
import com.kqzc.transportcar.interfaceListener.DialogButtonClickListener

/**
 * Created by on 2021/4/8.
 * 天天挂线
 *  746608431@qq.com
 *  描述:TODO
 */

abstract class BaseActivity<VM : BaseViewModel, DB : ViewDataBinding> : BaseVMDbActivity<VM, DB>() {
    private var dialogTwoButton: MaterialDialog? = null


    protected fun showDialogTwoButton(
        title: String,
        message: String,
        buttonNames: Array<String>,
        dialogButtonClickListener: DialogButtonClickListener
    ) {

        dialogTwoButton = this.let {
            MaterialDialog(it)
                .cancelable(false)
                .cancelOnTouchOutside(false)
                .customView(
                    R.layout.dialog_two_button_layout, null,
                    scrollable = true,
                    noVerticalPadding = true,
                    horizontalPadding = false,
                    dialogWrapContent = true
                )

        }
        dialogTwoButton?.getCustomView()?.run {
            this.findViewById<TextView>(R.id.tv_dialog_title).text = title
            this.findViewById<TextView>(R.id.tv_dialog_message).text = message

            if (buttonNames != null && buttonNames.isNotEmpty()) {
                when (buttonNames.size) {

                    2 -> {

                        this.findViewById<TextView>(R.id.tv_dialog_cancle).apply {
                            text = buttonNames[0]
                            setOnClickListener {
                                dialogTwoButton?.dismiss()
                                dialogButtonClickListener.negativeButtonClick()
                            }
                        }
                        this.findViewById<TextView>(R.id.tv_dialog_ok).apply {
                            text = buttonNames[1]
                            setOnClickListener {
                                dialogTwoButton?.dismiss()
                                dialogButtonClickListener.positiveButtonClick()
                            }
                        }
                    }


                    1 -> {
                        this.findViewById<TextView>(R.id.tv_dialog_cancle).apply {
                            visibility = View.GONE
                        }
                        this.findViewById<View>(R.id.line_mid).apply {
                            visibility = View.GONE
                        }

                        this.findViewById<TextView>(R.id.tv_dialog_ok).apply {
                            text = buttonNames[0]
                            setOnClickListener {
                                dialogTwoButton?.dismiss()
                                dialogButtonClickListener.positiveButtonClick()
                            }
                        }
                    }
                }

            }

        }
        dialogTwoButton?.show()
    }


    override fun onDestroy() {
        super.onDestroy()
        dialogTwoButton?.dismiss()
    }


    override fun showLoading(message: String) {
        showLoadingExt()
    }

    override fun dismissLoading() {
        dismissLoadingExt()

    }

    protected fun handleThrowable(apiException: ApiException) {
        var code = apiException.errCode
        var msg = apiException.errorMsg
        LogUtils.d("handleThrowable==code==${code}")
        msg?.let {
            ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(it)
        }

        when (code) {
            401 -> {//退出登录


            }

        }
    }

    protected fun toastMsg(message: String?) {
        message?.let {
            ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(it)
        }
    }


    /**
     * 启动应用的设置
     *
     * @since 2.5.0
     */
    private fun startAppSettings() {
        try {
            val intent = Intent(
                Settings.ACTION_APPLICATION_DETAILS_SETTINGS
            )
            intent.data = Uri.parse("package:$packageName")
            startActivity(intent)
        } catch (e: Throwable) {
            e.printStackTrace()
        }
    }

    private fun stringChangeColor(
        textView: TextView,
        allString: String,
        changeString: String,
        otherString: String
    ) {
        val fstart = allString.indexOf(changeString)
        val fend = fstart + changeString.length
        val style = SpannableStringBuilder(allString)
        if ("" != otherString && otherString != null) {
            val bstart: Int = allString.indexOf(otherString)
            val bend: Int = bstart + otherString.length
            style.setSpan(
                ForegroundColorSpan(Color.parseColor("#586E98")),
                bstart,
                bend,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE
            )
        }

        style.setSpan(
            ForegroundColorSpan(Color.parseColor("#E64243")),
            fstart,
            fend,
            Spannable.SPAN_EXCLUSIVE_INCLUSIVE
        )
        textView.setText(style)
    }
}